package com.heart.sell.controller.backend;

import com.heart.sell.config.ProjectUrlConfig;
import com.heart.sell.constant.CookConstant;
import com.heart.sell.constant.RedisConstant;
import com.heart.sell.enums.ResponseCodeEnum;
import com.heart.sell.pojo.SellerInfo;
import com.heart.sell.service.SellerService;
import com.heart.sell.util.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author Luo Zhen
 * @create 2019-04-30 16:11
 */
@Controller
@RequestMapping("/seller")
public class SellerUserController {

    @Autowired
    private SellerService sellerService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ProjectUrlConfig projectUrlConfig;

    @GetMapping("/login")
    public ModelAndView login(@RequestParam("openid") String openid,
                              HttpServletResponse response,
                              Map<String, Object> map) {
        // 1. 验证openid和数据库匹配
        SellerInfo sellerByOpenid = sellerService.findSellerByOpenid(openid);
        if (sellerByOpenid == null) {
            map.put("msg", ResponseCodeEnum.LOGIN_FAIL);
            map.put("url", "/seller/login");
            return new ModelAndView("common/error", map);
        }
        // 2. 设置token至redis
        String token = UUID.randomUUID().toString();
        Integer expireTime = RedisConstant.EXPIRE;
        redisTemplate.opsForValue().set(
                RedisConstant.TOKEN_PREFIX + token,
                openid, expireTime, TimeUnit.SECONDS);
        // 3. 设置token至cookie
        CookieUtil.set(response, CookConstant.TOKEN, token, expireTime);
        return new ModelAndView("redirect:" + projectUrlConfig.getSell() + "/sell/seller/order/list");
    }

    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request, HttpServletResponse response,
                               Map<String, Object> map) {
        // 1.从cookie里查询
        Cookie cookie = CookieUtil.get(request, CookConstant.TOKEN);
        if (cookie != null) {
            // 2.清除redis
            redisTemplate.opsForValue().getOperations().delete(RedisConstant.TOKEN_PREFIX + cookie.getValue());
            // 3.清除cookie(把原有键值对设置成0 即可)
            CookieUtil.set(response, CookConstant.TOKEN, null, 0);
        }
        map.put("msg", ResponseCodeEnum.LOGOUT);
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("common/success", map);
    }
}
